package com.jarchie.androiddev.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.jarchie.androiddev.R
import com.jarchie.androiddev.databinding.FragmentHomeBinding
import com.jarchie.androiddev.ext.invokeViewBinding
import com.jarchie.androiddev.ext.invokeViewModel
import com.jarchie.androiddev.ext.setVisibility
import com.jarchie.androiddev.http.ApiService
import com.jarchie.androiddev.model.HomeData
import com.jarchie.androiddev.model.HomeItem
import com.jarchie.androiddev.ui.adapters.HomeAdapter
import com.jarchie.androiddev.ui.item.BannerDataItem
import com.jarchie.androiddev.ui.item.CommonAdapter
import com.jarchie.androiddev.ui.item.CommonDataItem
import com.jarchie.androiddev.ui.item.TestDataItem
import kotlinx.coroutines.launch

class HomeFragment : Fragment() {
    private val binding: FragmentHomeBinding by invokeViewBinding()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.loading.setVisibility(true)
        binding.recyclerview.setVisibility(false)
        binding.recyclerview.layoutManager = LinearLayoutManager(requireContext())

        lifecycleScope.launch {
            val apiResult = ApiService.getService().requestHomeData()
            val homeData = apiResult.body as HomeData
            binding.loading.setVisibility(false)
            binding.recyclerview.setVisibility(true)
            val dataList = listOf(HomeItem.BannerItem(homeData.bannerList),
                HomeItem.ImageItem(homeData.images),
                HomeItem.VideoItem(homeData.videos))
            val mAdapter = HomeAdapter(dataList)
            binding.recyclerview.adapter = mAdapter
        }

//        val adapter= CommonAdapter(requireContext())
//        val dataSets = ArrayList<CommonDataItem<*,*>>()
//        val arr = arrayOf(1,2,3)
//        dataSets.add(TestDataItem(arr))
//        adapter.addItems(dataSets,false)
//        dataSets.add(BannerDataItem(homeData.bannerList))
//        dataSets.add(TestDataItem(homeData.bannerList))
//        adapter.addItems(dataSets,false)
    }

}